# Create your models here.
# course/py

from datetime import datetime

# from DjangoUeditor.models import UEditorField
from django.db.models import *
from users.models import User
from organization.models import Teacher, CourseOrg

class CourseCategory(Model):
    is_del = IntegerField(default=0)
    '''
    课程分类
    '''
    pid = IntegerField()
    name = CharField("类别名",max_length=50)
    level = IntegerField()
    path = CharField("分级路径",max_length=150)
    add_time = DateTimeField("添加时间",default=datetime.now,)

    class Meta:
        verbose_name = "课程分类"
        verbose_name_plural = verbose_name

class Course(Model):
    is_del = IntegerField(default=0)
    '''
    课程
    '''
    DEGREE_CHOICES = (
        ("cj", "初级"),
        ("zj", "中级"),
        ("gj", "高级")
    )
    name = CharField("课程名",max_length=50)
    description = CharField("课程描述",max_length=300)
    introduce = TextField("课程简介")
    degree = CharField('难度',choices=DEGREE_CHOICES, max_length=2)
    image = ImageField("封面图", upload_to="courses/%Y/%m", max_length=100)
    tag = CharField("标签",max_length=50)
    is_banner = BooleanField("是否轮播",default=False)
    course_org = ForeignKey(CourseOrg,verbose_name='所属机构',on_delete=CASCADE)
    category = ForeignKey(CourseCategory, verbose_name="课程目录",on_delete=CASCADE)
    teacher = ForeignKey(Teacher, verbose_name="讲师",on_delete=CASCADE)
    youneed_know = CharField("课程须知",max_length=300)
    teacher_tell = CharField("老师告诉你",max_length=300)
    learn_times = IntegerField("学习时长(分钟数)",default=0)
    students = IntegerField("学习人数",default=0)
    fav_nums = IntegerField("收藏人数",default=0)
    click_nums = IntegerField("点击数",default=0)
    money = DecimalField("课程价格",max_digits = 10,decimal_places = 2)
    add_time = DateTimeField("添加时间",default=datetime.now,)

    class Meta:
        verbose_name = "课程"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
class Lesson(Model):
    is_del = IntegerField(default=0)
    '''
    课程章节
    '''
    course = ForeignKey(Course,verbose_name='课程',on_delete=CASCADE)
    name = CharField("章节名",max_length=100)
    add_time = DateTimeField("添加时间",default=datetime.now)

    class Meta:
        verbose_name = "章节"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '《{0}》课程的章节 >> {1}'.format(self.course, self.name)

class Video(Model):
    is_del = IntegerField(default=0)
    '''
    章节视频
    '''
    lesson = ForeignKey(Lesson, verbose_name="章节",on_delete=CASCADE)
    name = CharField("视频名",max_length=100)
    url = CharField("学习时长",max_length=100)
    learn_times = IntegerField("学习时长(分钟)",default=0)
    add_time = DateTimeField("添加时间", default=datetime.now)

    class Meta:
        verbose_name = "视频"
        verbose_name_plural = verbose_name


class CourseResource(Model):
    is_del = IntegerField(default=0)
    '''
    课程资源
    '''
    course = ForeignKey(Course, verbose_name="课程",on_delete=CASCADE)
    name = CharField("名称",max_length=100)
    download = FileField("资源文件",upload_to="course/resource/%Y/%m",max_length=100)
    add_time = DateTimeField("添加时间", default=datetime.now)

    class Meta:
        verbose_name = "课程资源"
        verbose_name_plural = verbose_name

class Order(Model):
    is_del = IntegerField(default=0)
    '''
    订单
    '''
    ordersn = CharField("订单 编号",max_length=30)
    PAY_METHOD = (
        ("wechat", "微信支付"),
        ("alipay", "支付宝支付"),
    )
    pay_method = CharField("支付方式",choices=PAY_METHOD,max_length=30)
    price = DecimalField("订单价格",max_digits = 10,decimal_places = 2)
    pay_time = DateTimeField("支付时间", default=None,null=True, blank=True)
    course = ForeignKey(Course, verbose_name='课程', on_delete=CASCADE)
    user = ForeignKey(User,verbose_name='用户',on_delete=CASCADE)
    teacher = ForeignKey(Teacher, verbose_name="讲师", on_delete=CASCADE)
    origanization = ForeignKey(CourseOrg, verbose_name="机构", on_delete=CASCADE)
    add_time = DateTimeField("添加时间", default=datetime.now)

    class Meta:
        verbose_name = "订单"
        verbose_name_plural = verbose_name